/***************************************************************************
 *   Copyright (C) 2008 by Philipp Müller                                  *
 *   philipp.mueller@gmx.de                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
 ***************************************************************************/

#include <QtCore>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFormLayout>
#include <QGroupBox>
#include <QPushButton>

#include "widgetoptions.h"

WidgetOptions::WidgetOptions( VDR* vdr, QWidget *parent ) : QWidget( parent ),
    m_vdr( vdr )
{
    QVBoxLayout* vBox = new QVBoxLayout();
    QHBoxLayout* hBox = new QHBoxLayout();

    QFormLayout* formLayout = new QFormLayout();

    m_ftaCombo = new QComboBox( this );
    m_ftaCombo->addItems( QStringList() << "All" << "FTA only" );

    formLayout->setRowWrapPolicy( QFormLayout::DontWrapRows );
    formLayout->setFieldGrowthPolicy( QFormLayout::FieldsStayAtSizeHint );
    formLayout->setFormAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
    formLayout->setLabelAlignment( Qt::AlignLeft );

    formLayout->addRow( tr( "Display channels:" ), m_ftaCombo );

    QGroupBox* groupBox = new QGroupBox( tr( "Options" ), this );
    groupBox->setLayout( formLayout );

    vBox->addStretch();

    hBox->addStretch();
    hBox->addWidget( groupBox );
    hBox->addStretch();

    vBox->addLayout( hBox );

    vBox->addStretch();

    QPushButton* backButton = new QPushButton( tr( "Back" ), this );
    backButton->setIcon( style()->standardIcon( QStyle::QStyle::SP_ArrowLeft ) );
    vBox->addWidget( backButton );

    connect( backButton, SIGNAL( clicked() ), this, SLOT( backClicked() ) );

    setLayout( vBox );
}


WidgetOptions::~WidgetOptions()
{
}


void WidgetOptions::backClicked()
{
    if( m_ftaCombo->currentIndex() != m_displayChannelOrig )
    {
        if( m_ftaCombo->currentIndex() == 0 )
        {
            m_vdr->saveConfig( "General", "Channels", "All" );
        }
        else
        {
            m_vdr->saveConfig( "General", "Channels", "FTA only" );
        }
    }
    emit back();
}


void WidgetOptions::loadOptions()
{
    if( !m_vdr )
    {
        return;
    }

    //Display Channels
    m_displayChannel = m_vdr->loadConfig( "General", "Channels" );

    qDebug() << "WidgetOptions::loadOptions: displayChannels is now: " << m_displayChannel;

    if ( m_displayChannel.isEmpty() )
    {
        m_displayChannel = "All";
    }

    if ( m_displayChannel == "All" )
    {
        m_displayChannelOrig = 0;
    }
    else
    {
        m_displayChannelOrig = 1;
    }
    m_ftaCombo->setCurrentIndex( m_displayChannelOrig );
}


void WidgetOptions::saveOptions()
{
    if( !m_vdr )
    {
        return;
    }

}


